//
//  DrawerViewController.m
//  拖小延闹钟
//
//  Created by rimi on 15/10/8.
//  Copyright (c) 2015年 Liukai. All rights reserved.
//

#import "DrawerViewController.h"
#define LEFT_W 300

@interface DrawerViewController ()
{
    CGFloat scalef;
}
@property (nonatomic, strong) UIViewController *mainVC;
@property (nonatomic, assign) CGFloat speed;
@property (nonatomic, strong) UIViewController *leftVC;
@property (nonatomic, strong) UIViewController *rightVC;
@property (nonatomic, strong) UIScreenEdgePanGestureRecognizer *edgePanGesture;
@property (nonatomic, strong) MyRatioTool *ratioTool;
@end

@implementation DrawerViewController

- (void)viewDidLoad {
    self.view.backgroundColor = [UIColor colorWithWhite:0.757 alpha:1.000];
    [super viewDidLoad];
    [self initInterface]; 
    
}

- (void)initInterface {
    [self addChildViewController:self.mainVC];
    [self.view addSubview:self.mainVC.view];
    [self addChildViewController: self.leftVC];
    [self.view addSubview:self.leftVC.view];
    self.leftVC.view.frame = CGRectMake(-LEFT_W * (SCREEN_W / WIDTH) , 0, LEFT_W * (SCREEN_W / WIDTH), CGRectGetHeight(self.view.bounds));
    [self.mainVC.view addGestureRecognizer:self.edgePanGesture];
    
    UITapGestureRecognizer *pan = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlePanGesture:)];
    [self.mainVC.view addGestureRecognizer:pan];
}
- (instancetype)initWithMainViewController:(UIViewController *)mainViewController LeftViewController:(UIViewController *)leftViewController {
    self = [super init];
    if (self) {
            _speed = 0.5;
        _leftVC = leftViewController;
        _mainVC = mainViewController;
    }
    return self;
}

- (UIScreenEdgePanGestureRecognizer *)edgePanGesture {
    if (!_edgePanGesture) {
        _edgePanGesture = ({
            UIScreenEdgePanGestureRecognizer *pan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(action_edgePanGesture:)];
            pan.edges = UIRectEdgeLeft;
            pan;
        });
    }
    return _edgePanGesture;
}

- (void)handlePanGesture:(UIPanGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [self showMainView];
    }
}
- (void)openLeftViewController {
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    self.mainVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,0.8,0.8);
    self.mainVC.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width + 63,[UIScreen mainScreen].bounds.size.height/2);
    [UIView commitAnimations];
    [UIView animateWithDuration:0.5 animations:^{
      self.leftVC.view.frame = CGRectMake(0, 0, LEFT_W * (SCREEN_W / WIDTH), CGRectGetHeight(self.view.frame));
    }];
    scalef = 0;
}

-(void)showMainView{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    self.mainVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1.0,1.0);
    self.mainVC.view.center = CGPointMake([UIScreen mainScreen].bounds.size.width/2,[UIScreen mainScreen].bounds.size.height/2);
    [UIView commitAnimations];
    [UIView animateWithDuration:0.5 animations:^{
            self.leftVC.view.frame = CGRectMake(-LEFT_W, 0, LEFT_W, CGRectGetHeight(self.leftVC.view.frame));
    }];
    scalef = 0;
}

- (void)action_edgePanGesture:(UIScreenEdgePanGestureRecognizer *)gesture {
    CGFloat velocity = [gesture translationInView:self.view].x;
    scalef = (velocity * _speed + scalef);
    if (gesture.state == UIGestureRecognizerStateBegan) {
        if (velocity < 0) {
            return;
        }
    }
       if(gesture.view.frame.origin.x >= 0) {
        gesture.view.transform = CGAffineTransformScale(CGAffineTransformIdentity,1,1 - scalef / 1000);
        gesture.view.center = CGPointMake(gesture.view.center.x + velocity * _speed,gesture.view.center.y);
        [gesture setTranslation:CGPointMake(0, 0) inView:self.view];
           self.leftVC.view.center = CGPointMake(self.leftVC.view.center.x + velocity * _speed, self.leftVC.view.center.y);
           self.leftVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0);
    }
    if(gesture.state == UIGestureRecognizerStateEnded || gesture.state == UIGestureRecognizerStateCancelled) {
        if (scalef > 140 * _speed) {
            [self openLeftViewController];
        }
        else {
            [self showMainView];
            scalef = 0;
        }
        
    }
}


@end
